﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Persistente.Cabelo
{
    public class PCorCabelo
    {
        private DB_SIMETRIA _db;
        private SqlConnection _conexao = new SqlConnection(ConfigurationManager.ConnectionStrings["DB_SIMETRIA_SQL"].ConnectionString);


        public bool Salvar(string nmCorCabelo)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var tbCorCabelo = new tb_cor_cabelo();
                tbCorCabelo.nm_cor_cabelo = nmCorCabelo;
                _db.AddTotb_cor_cabelo(tbCorCabelo);
                _db.SaveChanges();

                return true;

            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

        public DataSet pegarCoresCabelo(string nmCorCabelo)
        {
            var ds = new DataSet();

            string query = "SELECT ";
            query += " id_cor_cabelo";
            query += ", nm_cor_cabelo";
            query += " FROM tb_cor_cabelo";

            query += " WHERE 1 = 1";


            if (!string.IsNullOrEmpty(nmCorCabelo))
            {
                query += " AND nm_cor_cabelo LIKE '%" + nmCorCabelo + "%'";
            } 

            var cmd = new SqlCommand(query, _conexao);

            cmd.Connection = _conexao;

            if (_conexao.State == ConnectionState.Closed)
            {
                _conexao.Open();
            }

            var dataAdapter = new SqlDataAdapter(cmd);
            dataAdapter.Fill(ds);

            _conexao.Close();

            return ds;
        }

        public bool Remover(int idCorCabelo)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var obTbCorCabelo = _db.tb_cor_cabelo.Where(q => q.id_cor_cabelo == idCorCabelo).FirstOrDefault();
                if (obTbCorCabelo == null)
                {
                    return false;
                }
                else
                {
                    _db.tb_cor_cabelo.DeleteObject(obTbCorCabelo);
                    _db.SaveChanges();
                    return true;
                }

            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

        public tb_cor_cabelo pegarCorCabeloPorId(int idCorCabelo)
        {
            try
            {
                _db = new DB_SIMETRIA();
                return _db.tb_cor_cabelo.Where(q => q.id_cor_cabelo == idCorCabelo).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

        public bool Alterar(int idCorCabelo, tb_cor_cabelo obTbCorCabeloNovo)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var obTbCorCabeloVelho =
                    _db.tb_cor_cabelo.Where(q => q.id_cor_cabelo == idCorCabelo).FirstOrDefault();

                obTbCorCabeloVelho.nm_cor_cabelo = obTbCorCabeloNovo.nm_cor_cabelo;

                _db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                _db.Connection.Close();
            }
        }
    }
}
